<template>
	<view>
		<view class="section">
			<view class="section-1 flex-align">
				<view class="image">
					<view
						style="width: 100%;height: 250rpx;background-color: #EDF5F5;display: flex;lex-direction: row; justify-content: center; align-items: center;">
						<image style="width: 100%;height: 250rpx;" :src="datalist.cover" mode="aspectFit"></image>
					</view>
				</view>

				<view class="detail">
					<view>
						<view class="title elipsis-1">{{ datalist.name }}</view>
						<view class="" style="display: flex; flex-direction: row; align-items: center;">
							<view class="tag flex-align">
								{{ datalist.course_name }}
							</view>
							<!-- <image style="width: 25rpx;height: 25rpx;"
								src="https://jiangsudiantuo2022.oss-cn-hangzhou.aliyuncs.com/img/f1c02d6507d5b869a8fa25b144993dc02a42e038.png"
								mode=""></image>
							<view style="color: #FE8C61;font-size: 24rpx;margin-left: 6rpx;">
								招生中
							</view> -->
						</view>

						<view class="flex-between">
							<view style="font-size: 24rpx;color: #4E6EAF;margin-top: 20rpx;">
								{{datalist.display_time}}
							</view>
							<view class="" style="font-size: 24rpx;color: #ff0000;" v-if="hour_num">
								{{hour_num}}次
							</view>
						</view>
						<view class="flex-between">
							<view style="font-size: 24rpx;color: #4E6EAF;margin-top: 20rpx;">
								{{datalist.area_name}}
							</view>
							<view class="" style="font-size: 24rpx;color: #ff0000;">
								剩余：{{less_stock?less_stock:0}}
							</view>
						</view>
						<view class="flex-between">
							<view
								style="font-size: 24rpx;color: #4E6EAF;margin-top: 20rpx;display: flex; flex-direction: row; ">
								<view class="">
									剩余：{{ datalist.less_stock }}
								</view>
								<view class="tags flex-align">
									{{ datalist.train_enum_name }}
								</view>
							</view>
							<view class="" style="font-size: 24rpx;color: #ff0000;">
								{{price_selling}}
							</view>
						</view>
					</view>
					<!-- <view class="flex-align">
						<view class="price">{{item.area_name}}</view>
					</view> -->
				</view>
			</view>

			<view style="margin-top: 20rpx;">
				<view class="section-2" v-for="(im, ix) in skuList" :key="ix">
					<view class="sub-title">{{ im.name }}</view>
					<view class="tag-list">
						<view v-for="(item2, index2) in im.list" :key="index2" @click="clickSpec(im, item2)"
							style="display: flex;flex-direction: column; ;align-items: center;  justify-content: center; flex-wrap: wrap;padding-right: 20rpx;">
							<view v-if="item2.coach_avatar"
								:class="[hasSelected(item2)? 'active-menu-image':'menu-image']">
								<image style="border-radius: 50% 50%;" :src="item2.coach_avatar" mode="scaleToFill" />
							</view>
							<view class="spec-value" >
								{{ item2.sku_name }}
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="" style="width: 100%;height: 140rpx;"></view>
			<view class="footer flex-align">
				<view class="btn" @click="handSubmit" v-if="less_stock > 0">开始报名</view>
				<view class="admbtn" v-else>开始报名</view>
			</view>
		</view>
	</view>
</template>

<script>
	const venues = require("@/api/venues/venues.js");
	import conAlert from "@/components/com-alert/com-alert.vue";
	export default {
		components: {
			conAlert,
		},
		data() {
			return {
				datalist: {},
				coachList: [],
				courseItemList: [],
				trainid1: '',
				trainid2: '',
				occupation_relation: '',
				occupation_relation_id: '',
				showoccupation: false,
				occupation_status: [],
				price_selling: '',
				hour_num: '',
				venue_id: '',
				courselist: {},
				venue_name: '',
				less_stock: '',
				trainidname2: '',
				trainidname1: '',
				skuOplist: [],
				trainid3: '',
				skuList: [],
				checkedSpec: [],
				checkedSpecStr: "",
				selectedId1: null, // 存储课包选择的id
				selectedId2: null, // 存储教练选择的id
				defaultCombinedValue: "" // 用于存储默认拼接值
			};
		},

		onLoad(options) {
			const selectionArrs = JSON.parse(decodeURIComponent(options.selectionArr))
			this.datalist = selectionArrs
			this.venue_id = options.venue_id;
			this.venue_name = options.venue_name;
			this.trainitems()
			this.sku_options()
		},
	
		methods: {
			hasSelected(item) {
				return item.selected === true;
			},
			clickSpec(im, item2) {
				if (im.id === 1) {
					if (im.name === "课包选择") {
						this.selectedId1 = item2.id;
					} else if (im.name === "教练选择") {
						this.selectedId2 = item2.id;
					}
				} else if (im.id === 0) {
					let groupValues = [];
					this.skuList.forEach((obj) => {
						if (obj.id === 0) {
							const clickedItem = obj.list.find(i => i === item2);
							groupValues.push(clickedItem || (obj.list.length > 0 ? obj.list[0] : ""));
						}
					});
					if (this.selectedId1 && this.selectedId2) {
						let updatedResult = `${this.selectedId1}-${this.selectedId2}-${groupValues.join('')}`;
						console.log(updatedResult, 'updatedResult');
					}
				}
			},
			
			getGroupValues() {
				let groupValues = [];
				this.skuList.forEach((obj) => {
					if (obj.id === 0) {
						const item2 = this.skuList.flatMap(o => o.list).find(i => i);
						const clickedItem = obj.list.find(i => i === item2);
						groupValues.push(clickedItem || (obj.list.length > 0 ? obj.list[0] : ""));
					}
				});
				return groupValues;
			},

			async sku_options() {
				let that = this
				let params = {
					train_id: this.datalist.id,
				}
				let res = await venues.sku_list(params)
				if (res.code === 1) {
					this.skuList = res.data.skuList
					this.defaultCombinedValue = this.getGroupFirstItems().join('-');
					console.log(this.defaultCombinedValue, 'this.defaultCombinedValue', this.getGroupFirstItems());
				}
			},
			
			getGroupFirstItems() {
				let groupFirstItems = [];
				this.skuList.forEach((obj) => {
					if (obj.id == 1) {
						groupFirstItems.push((obj.list)[0].id);
					}
					if (obj.id === 0 && obj.list.length > 0) {
						groupFirstItems.push((obj.list)[0].sku_name);
					}
				});
				return groupFirstItems;
			},

			async trainitems() {
				let params = {
					train_id: this.datalist.id,
				}
				let res = await venues.trainitems(params)
				if (res.code === 1) {
					this.coachList = res.data.coachList
					this.courseItemList = res.data.courseItemList
				}
			},

			handSubmit() {
				console.log(this.courselist, 'this.courselist')
				uni.navigateTo({
					url: `./train_student?selectionArr=${encodeURIComponent(JSON.stringify(this.datalist))}` +
						"&venue_id=" + this.venue_id + "&courselist=" + encodeURIComponent(JSON.stringify(this
							.courselist)) +
						"&coach_id=" + this.trainid2 + "&trainidname1=" + this.trainidname1 + "&trainidname2=" +
						this.trainidname2 + "&venue_name=" + this.venue_name + "&course_item_id=" + this.trainid1
				})
			}
		},
	};
</script>

<style lang="scss" scoped>
	.tag-list {
		display: flex;
		flex-wrap: wrap;
	}

	.spec-value {
		padding: 5rpx 10rpx;
		margin: 5px;
		border: #f0f0f0 1rpx solid;
		font-size: 24rpx;
	}

	.spec-value1 {
		padding: 5rpx 10rpx;
		margin: 5px;
		border: #ffffff 1rpx solid;
		font-size: 24rpx;
	}

	.text_color {
		padding: 5rpx 10rpx;
		margin: 5px;
		color: #2672eb;
		border: 1rpx solid #2672eb;
		font-size: 24rpx;
	}

	.text_color1 {
		padding: 5rpx 10rpx;
		margin: 5px;
		color: #2672eb;
		border: 1rpx solid #ffffff;
		font-size: 24rpx;
	}

	.btns {
		cursor: pointer;
		padding: 10px;
		margin-top: 20px;
		text-align: center;
		background-color: #3498db;
		color: #fff;
	}

	.menu-image {
		width: 100rpx;
		height: 100rpx;
		background: url('https://jiangsudiantuo2022.oss-cn-hangzhou.aliyuncs.com/img/60cb40ff572199778113a809978ef6aa0f09db36.png') 100% no-repeat;
		background-size: 100% 100%;
		display: flex;
		justify-content: center;
		align-items: center;

		image {
			width: 60rpx;
			height: 60rpx;
		}
	}

	.menu-image-no-checked {
		width: 100rpx;
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		image {
			width: 60rpx;
			height: 60rpx;
		}
	}


	.section {
		padding: 24rpx;

		.select-list {
			.item {
				margin-bottom: 20rpx;
				padding-bottom: 20rpx;
				height: 50rpx;
				border-bottom: 1px solid #cccccc;
				padding: 0;

				&:last-of-type {
					border: none;
					margin: 0;
					padding: 0;
				}

				.image {
					width: 150rpx;
					height: 100%;

					/deep/ .vue-ref {
						width: 100%;
						height: 100%;
					}
				}

				.detail {
					display: flex;
					height: 100%;
					flex: 1;
					width: 0;
					margin-left: 20rpx;
					flex-direction: column;
					justify-content: space-between;

					.name {
						font-weight: 700;
						font-size: 28rpx;
						margin-bottom: 15rpx;
					}

					.timer {
						color: #cacaca;
						font-size: 20rpx;
					}

					.bottom {
						font-size: 20rpx;
						color: #101010;

						.address {
							width: 70%;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
						}
					}
				}
			}
		}

		.btns {
			background: linear-gradient(0deg, #528EFF, #2F75FA);
			border-radius: 20px;
			font-size: 28rpx;
			font-weight: bold;
			color: #FFFFFF;
			// padding: 26rpx 0;
			// margin: 0px 30rpx;
			// margin-top: 120rpx;
			text-align: center;
			// position: fixed;
			// bottom: 24px;
			// right: 5%;
			width: 90%;
			height: 80rpx;
			line-height: 80rpx;
		}

		.btns_m {
			background: linear-gradient(0deg, #528EFF, #2F75FA);
			border-radius: 5px;
			font-size: 28rpx;
			font-weight: bold;
			color: #FFFFFF;
			padding: 26rpx 0;
			width: 160rpx;
			height: 60rpx;
			text-align: center;
			line-height: 12rpx;

		}

		.btns_ms {
			// position: absolute;
			// right: 10rpx;
			// top: 110rpx;
		}

		.btns_s {
			background: linear-gradient(0deg, #bebebe, #bebebe);
			border-radius: 20px;
			font-size: 28rpx;
			font-weight: bold;
			color: #FFFFFF;
			// padding: 26rpx 0;
			// margin: 0px 30rpx;
			// margin-top: 120rpx;
			text-align: center;
			// position: fixed;
			// bottom: 24px;
			// right: 5%;
			width: 90%;
			height: 80rpx;
			line-height: 80rpx;
		}

		.footer {
			position: fixed;
			width: 100%;
			background-color: white;
			bottom: 0;
			left: 0;
			padding: 20rpx 25rpx 20rpx 25rpx;
			box-sizing: border-box;

			.menu {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 40rpx;

				image {
					width: 35rpx;
					height: 35rpx;
				}

				text {
					color: rgb(51, 51, 51);
					font-size: 24rpx;
					font-weight: 700;
					margin-top: 13rpx;
				}
			}

			.btn {
				flex: 1;
				color: rgb(255, 255, 255);
				font-size: 30rpx;
				padding: 26rpx 0;
				border-radius: 60rpx;
				background-color: #3e80fc;
				text-align: center;
				font-weight: 700;
			}

			.admbtn {
				flex: 1;
				color: rgb(255, 255, 255);
				font-size: 30rpx;
				padding: 26rpx 0;
				border-radius: 60rpx;
				background-color: #bcbcbc;
				text-align: center;
				font-weight: 700;
			}
		}

		.section-1 {
			height: 250rpx;

			.image {
				width: 200rpx;
				height: 250rpx;
				margin-right: 26rpx;
				border-radius: 10rpx;
				overflow: hidden;

				image {
					width: 100%;
					height: 250rpx;
				}
			}

			.detail {
				flex: 1;
				width: 0;
				display: flex;
				height: 100%;
				flex-direction: column;
				justify-content: space-between;

				.detail-1 {
					font-size: 27rpx;
					font-weight: 500;
					color: #333333;
				}

				.title {
					font-size: 28rpx;
					margin-bottom: 10rpx;
					width: 100%;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}

				.tag {
					font-size: 22rpx;
					color: #2F75FA;
					background-color: #d5e3fe;
					border-radius: 8rpx;
					padding: 5rpx 10rpx;
					margin-right: 20rpx;
				}

				.tags {
					font-size: 22rpx;
					color: #2F75FA;
					background-color: #d5e3fe;
					border-radius: 8rpx;
					padding: 5rpx 10rpx;
					margin-left: 20rpx;
				}

				.detail-2 {
					font-size: 24rpx;
					font-weight: 500;
					color: #888888;
					margin-top: 12rpx;
				}

				.price {
					font-size: 20rpx;
					font-weight: 500;
					color: #ff0000;

					text {
						font-size: 32rpx;
						font-weight: 700;
					}
				}
			}
		}

		.section-2 {
			margin-top: 30rpx;

			.top-menu-list {
				display: flex;
				flex-wrap: wrap;

				.top-menu-item {
					display: flex;
					flex-direction: column;
					align-items: center;
					width: 20%;
					// margin-top: 10rpx;
					padding: 0px 30rpx;
					box-sizing: border-box;
					font-size: 25rpx;

					.add_na {
						display: flex;
						flex-direction: column;
						align-items: center;
						width: 20%;
						// margin-top: 30rpx;
						padding: 0px 30rpx;
						box-sizing: border-box;
						font-size: 25rpx;

						.menu-image {
							width: 100rpx;
							height: 100rpx;
							// border-radius: 37rpx;
							// background: rgb(250, 237, 234);
							// background: linear-gradient(45deg,
							// 		rgba(250, 237, 234, 1) 0%,
							// 		rgba(246, 227, 216, 1) 77%);
							background: url('https://jiangsudiantuo2022.oss-cn-hangzhou.aliyuncs.com/img/60cb40ff572199778113a809978ef6aa0f09db36.png') 100% no-repeat;
							background-size: 100% 100%;
							// position: absolute;
							// top: 0;
							// right: 0;
							// z-index: 999;
							display: flex;
							justify-content: center;
							align-items: center;

							image {
								width: 60rpx;
								height: 60rpx;
							}
						}

						.menu-image-no-checked {
							width: 100rpx;
							height: 100rpx;
							display: flex;
							justify-content: center;
							align-items: center;

							image {
								width: 60rpx;
								height: 60rpx;
							}
						}

						.no-checked-text {
							white-space: nowrap;
						}

						.checked-text {
							margin-top: 20rpx;
							white-space: nowrap;
							font-weight: bold;
						}
					}
				}
			}

			.sub-title {
				font-size: 28rpx;
				font-weight: bold;
				color: #333333;
				margin-bottom: 20rpx;
			}

			.tag-list {
				display: flex;
				flex-wrap: wrap;

				/deep/.u-tag {
					margin-left: 30rpx;
					margin-bottom: 30rpx;
				}

				.atinput {
					font-size: 26rpx;
					width: 600rpx;
					margin-left: 20rpx;
					margin: 5px;
				}

				// .tag{
				// 	background: #EFEFEF;
				// 	border-radius: 3px;
				// 	font-size: 24rpx;
				// 	font-weight: 500;
				// 	color: #FFFFFF;
				// 	padding: 7rpx 20rpx;
				// 	margin-right: 20rpx;
				// 	margin-bottom: 20rpx;
				// }

			}
		}



		.section-3 {
			font-size: 22rpx;
			font-weight: 500;
			color: #333333;
		}
	}
</style>